package com.springboot.mybatis.service.impl;

import com.springboot.mybatis.entity.UserMyBatis;
import com.springboot.mybatis.mapper.UserMyBatisMapper;
import com.springboot.mybatis.service.UserMyBatisMapperService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;

/*
 *DATE:2025/7/22
 */
@Service
public class UserMyBatisMapperServiceImpl implements UserMyBatisMapperService {

    @Resource
    private UserMyBatisMapper userMyBatisMapper;
    //@Transactional
    @Override
    public void addTwoUser(UserMyBatis user1, UserMyBatis user2) {
        userMyBatisMapper.insertOnre(user1);
        userMyBatisMapper.insertOnre(user2);
    }

    @Resource
    private TransactionTemplate transactionTemplate;

    @Override
    public void addTwoUser2(UserMyBatis user1, UserMyBatis user2) {
        transactionTemplate.executeWithoutResult(status -> {
            try {
                userMyBatisMapper.insertOnre(user1);
                userMyBatisMapper.insertOnre(user2);
            } catch (Exception e) {
                status.setRollbackOnly();
            }
        });
    }
}
